<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '../api/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()

const registerForm = reactive({
    username: '',
    password: '',
    confirmPassword: '',
    nickname: '',
    email: ''
})

const loading = ref(false)

const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value !== registerForm.password) {
                    callback(new Error('两次输入密码不一致'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ],
    nickname: [
        { required: true, message: '请输入昵称', trigger: 'blur' },
        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
    ],
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
    ]
}

const formRef = ref(null)

const handleRegister = async () => {
    if (!formRef.value) return
    
    await formRef.value.validate(async (valid) => {
        if (valid) {
            try {
                loading.value = true
                const { confirmPassword, ...registerData } = registerForm
                const res = await register(registerData)
                if (res.code === 200) {
                    ElMessage.success('注册成功')
                    router.push('/login')
                } else {
                    ElMessage.error(res.message || '注册失败')
                }
            } catch (error) {
                console.error('Register error:', error)
                ElMessage.error(error.response?.data?.message || '注册失败')
            } finally {
                loading.value = false
            }
        }
    })
}
</script>

<template>
    <div class="register-container">
        <div class="register-box">
            <h2>注册</h2>
            <el-form 
                ref="formRef"
                :model="registerForm"
                :rules="rules"
                label-width="0"
            >
                <el-form-item prop="username">
                    <el-input 
                        v-model="registerForm.username"
                        placeholder="用户名"
                        prefix-icon="User"
                    />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input 
                        v-model="registerForm.password"
                        type="password"
                        placeholder="密码"
                        prefix-icon="Lock"
                        show-password
                    />
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input 
                        v-model="registerForm.confirmPassword"
                        type="password"
                        placeholder="确认密码"
                        prefix-icon="Lock"
                        show-password
                    />
                </el-form-item>
                <el-form-item prop="nickname">
                    <el-input 
                        v-model="registerForm.nickname"
                        placeholder="昵称"
                        prefix-icon="User"
                    />
                </el-form-item>
                <el-form-item prop="email">
                    <el-input 
                        v-model="registerForm.email"
                        placeholder="邮箱"
                        prefix-icon="Message"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button 
                        type="primary" 
                        :loading="loading"
                        @click="handleRegister"
                        style="width: 100%"
                    >
                        注册
                    </el-button>
                </el-form-item>
                <div class="form-footer">
                    <router-link to="/login">已有账号？立即登录</router-link>
                </div>
            </el-form>
        </div>
    </div>
</template>

<style scoped lang="scss">
.register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: calc(100vh - 60px);
    background: #f4f5f5;

    .register-box {
        width: 400px;
        padding: 30px;
        background: #fff;
        border-radius: 4px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }

        .form-footer {
            text-align: center;
            margin-top: 20px;

            a {
                color: #409eff;
                text-decoration: none;

                &:hover {
                    text-decoration: underline;
                }
            }
        }
    }
}
</style> 